from fastapi import APIRouter, Body
from fastapi.params import Query, Path

from api.models.user_model import UserModel
from core.utils.json_result import JsonResult, ResultCodeEnum
from core.utils.page import QueryPage
from core.utils.query_model import QueryModel
from logger import log

user_router = APIRouter(prefix="/user", tags=["用户管理"])


@user_router.get("/list")
async def get_user_list():
    """
    查询用户列表
    :return:
    """
    log.info("hello...")
    return {"message": "user_list"}


@user_router.get("/get/{user_id}")
async def get_user_list(user_id: int):
    """
    使用url变量方式
    :param user_id:
    :return:
    """
    return {"message": "user_list", "id": user_id}


@user_router.get("/getvalidate/{user_id}")
async def get_user_list(user_id: int = Path(ge=1)):
    """
    使用url变量方式，并约束user_id参数必须大于等于1
    :param user_id:
    :return:
    """
    return {"message": "user_list", "id": user_id}


@user_router.get("/query")
async def create_user(user_id: int = Query(default=None, ge=1, le=10)):
    """
    类似于 query?user_id=xxx
    Query 约束参数验证:
        default: 设置为 None时，此参数可以不传
        ge: 值要大于等于指定值
        le: 值要小于等于指定值
    :param user_id:
    :return:
    """
    return {"message": "create_user", "id": user_id}


@user_router.post("/update")
async def update_user(user: UserModel = Body(default=None)):
    """
     request Body 自定义对象
    :param user:
    :return:
    """
    return JsonResult[UserModel](code=ResultCodeEnum.SUCCESS.value.value, message="OK", data=user)


@user_router.post("/page")
async def user_page(query: QueryModel[dict] = Body(default=None)):
    """
    列表查询
    :param query:
    :return:
    """
    print(query)
    page = QueryPage[dict].create_with_query_model(query, data=[], total=0)
    return JsonResult[QueryPage[dict]](code=ResultCodeEnum.SUCCESS.value.value,
                                       message=ResultCodeEnum.SUCCESS.value.label,
                                       data=page)
